#!/bin/bash

# OH一点通插件测试脚本
# 用于测试插件各项功能

set -e

echo "🧪 OH一点通插件功能测试"
echo "================================"

# 检查插件是否已安装
if ! code --list-extensions | grep -q "oh-yidian-tong"; then
    echo "❌ 错误: OH一点通插件未安装"
    echo "请先运行 install.sh 安装插件"
    exit 1
fi

echo "✅ 插件已安装"

# 检查微服务状态
echo "🔍 检查微服务状态..."
check_service() {
    local service_url=$1
    local service_name=$2
    
    if curl -s --connect-timeout 5 "$service_url" > /dev/null 2>&1; then
        echo "✅ $service_name 运行正常"
        return 0
    else
        echo "❌ $service_name 未运行或无法访问"
        return 1
    fi
}

services_running=0
total_services=6

check_service "http://localhost:8081/actuator/health" "代码补全服务" && ((services_running++))
check_service "http://localhost:8082/actuator/health" "代码生成服务" && ((services_running++))
check_service "http://localhost:8083/actuator/health" "代码分析服务" && ((services_running++))
check_service "http://localhost:8084/actuator/health" "LLM服务" && ((services_running++))
check_service "http://localhost:8085/actuator/health" "数据服务" && ((services_running++))
check_service "http://localhost:8086/actuator/health" "构建服务" && ((services_running++))

echo "微服务状态: $services_running/$total_services 个服务运行中"

if [ $services_running -lt 3 ]; then
    echo "⚠️  警告: 微服务运行不完整，可能影响插件功能"
    echo "请确保SmartCodeService微服务集群正在运行"
fi

# 测试API连接
echo ""
echo "🔌 测试API连接..."

test_api() {
    local url=$1
    local name=$2
    
    echo -n "测试 $name... "
    if curl -s --connect-timeout 10 "$url" > /dev/null 2>&1; then
        echo "✅ 成功"
        return 0
    else
        echo "❌ 失败"
        return 1
    fi
}

test_api "http://localhost:8081/completion/health" "代码补全API"
test_api "http://localhost:8082/generation/health" "代码生成API"
test_api "http://localhost:8083/analysis/health" "代码分析API"
test_api "http://localhost:8084/llm/health" "LLM API"
test_api "http://localhost:8085/data/health" "数据API"
test_api "http://localhost:8086/build/health" "构建API"

# 测试GLM4.6 API
echo ""
echo "🤖 测试GLM4.6 API..."
test_glm_api() {
    local api_key="df6af53dd93b4afe9c1f0781f198305a.zcW6GYuBCOz3Xb27"
    local test_prompt="Hello, this is a test message."
    
    echo -n "测试GLM4.6 API... "
    
    response=$(curl -s --connect-timeout 10 \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $api_key" \
        -d "{\"prompt\":\"$test_prompt\",\"model\":\"glm-4\"}" \
        "http://localhost:8084/api/llm/generate" 2>/dev/null)
    
    if echo "$response" | grep -q "code.*200"; then
        echo "✅ 成功"
        return 0
    else
        echo "❌ 失败"
        return 1
    fi
}

test_glm_api

# 创建测试文件
echo ""
echo "📝 创建测试文件..."
test_file="test_oh_yidian_tong.cpp"

cat > "$test_file" << 'EOF'
#include <iostream>
#include <vector>

namespace harmony {
namespace pilot {

class TestClass {
public:
    TestClass() = default;
    ~TestClass() = default;
    
    int add(int a, int b) {
        return a + b;
    }
    
    void printMessage(const std::string& message) {
        std::cout << message << std::endl;
    }
};

} // namespace pilot
} // namespace harmony

int main() {
    using namespace harmony::pilot;
    
    TestClass test;
    int result = test.add(5, 3);
    test.printMessage("Hello, OH一点通!");
    
    return 0;
}
EOF

echo "✅ 测试文件已创建: $test_file"

# 提供测试指导
echo ""
echo "🎯 手动测试指导:"
echo "1. 在VSCode中打开 $test_file"
echo "2. 测试代码续写功能:"
echo "   - 在函数内部按 Ctrl+Space"
echo "   - 输入 'test.' 查看补全建议"
echo "3. 测试代码生成功能:"
echo "   - 按 Ctrl+Shift+G"
echo "   - 输入: '生成一个计算阶乘的函数'"
echo "4. 测试单元测试生成:"
echo "   - 右键点击文件"
echo "   - 选择 '生成单元测试'"
echo "5. 测试Fuzz测试生成:"
echo "   - 右键点击文件"
echo "   - 选择 '生成Fuzz测试'"
echo "6. 测试代码分析:"
echo "   - 右键点击文件"
echo "   - 选择 '代码分析'"

echo ""
echo "📊 测试总结:"
echo "- 插件安装: ✅"
echo "- 微服务状态: $services_running/$total_services 个服务运行"
echo "- 测试文件: ✅ 已创建"
echo "- 手动测试: 请按照上述指导进行"

echo ""
echo "🔧 如果遇到问题:"
echo "1. 检查VSCode开发者控制台 (Ctrl+Shift+I)"
echo "2. 查看插件设置是否正确"
echo "3. 确保微服务正在运行"
echo "4. 验证网络连接和API密钥"

echo ""
echo "📚 更多帮助请查看README.md文件"
